import Vue from 'vue'
import Router from 'vue-router'
import routers from './routerData'
import nprogress from 'nprogress'
import 'nprogress/nprogress.css'
import { logout } from '@/api/loginout'
import { message } from 'ant-design-vue'
import store from '../store/index.js'
const originalPush = Router.prototype.push
Router.prototype.push = function push(location) {
  return originalPush.call(this, location).catch(err => err)
}

Vue.use(Router)

export const constantRoutes = routers

const createRouter = () =>
  new Router({
    // mode: 'history',
    scrollBehavior: () => ({ y: 0 }),
    routes: constantRoutes
  })

const router = createRouter()

nprogress.configure({ showSpinner: false })

// 在路由中使用
router.beforeEach((to, from, next) => {
  // console.log(to);
  // console.log(localStorage.getItem('zhpk-sessionId'));
  // console.log(process.env.VUE_APP_CAS_URL + '/logout?service=' + encodeURIComponent(store.state.appPublicUrl));

  //路由守卫
  // if (to.path != '/login') {
  //   if (localStorage.getItem('zhpk-sessionId') == null) {
  //     message.destroy()
  //     message.warning('会话失效，请重新登录。')
  //     logout({}).then((res) => {
  //       localStorage.clear()
  //       top.window.location =
  //         process.env.VUE_APP_CAS_URL + '/logout?service=' + encodeURIComponent(store.state.appPublicUrl)

  //     })
      
  //   }
  // }

  nprogress.start()
  next();
})

router.afterEach(() => {
  nprogress.done()
})

export function resetRouter() {
  // const newRouter = createRouter()
  router.matcher = newRouter.matcher // 重置 路由
}

export default router
